package com.sgb.kjwl.view.ui.fragment.pick;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppFragment;
import com.sgb.link.utils.PickCateType;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentPickAttentionSigningUpListBinding;
import com.sgb.kjwl.di.pick.DaggerPickProjectComponent;
import com.sgb.kjwl.di.pick.PickProjectModule;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.pick.TenderDetailsActivity3;
import com.sgb.kjwl.view.widget.pick.PickPopWindowView;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.TenderListEntity;
import com.sgb.kjwl.view.ui.activity.pick.PickNoticeDetailActivity;
import com.sgb.kjwl.view.ui.adapter.pick.PickHallBidingAdapter;
import com.sgb.kjwl.view.widget.CommonCategoryFilterPopWindow;
import com.sgb.kjwl.view.widget.PickHallBidingPopWindow;
import com.sgb.kjwl.view.widget.pick.PickChooseLeftDialog2;
import com.sgb.kjwl.viewmodel.PickAttentionCateType;
import com.sgb.kjwl.viewmodel.pick.PickAttentionSigningUpListModel;
import com.sgb.zhaocai.utils.PickCopyUtils;
import com.swgk.core.util.PickSelectTextEntity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.areapopup.GoodsSourceLocationEntity;
import com.swgk.core.view.areapopup.ThreeLevelAreaWindow;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;


/**
 * 智能关注-招采-招标
 */
public class PickAttentionSigningUpListFragment extends AppFragment implements PickChooseLeftDialog2.DialogCallBackImpl {

    @Inject
    PickAttentionSigningUpListModel viewModel;
    FragmentPickAttentionSigningUpListBinding binding;
    private PickHallBidingAdapter pickingAdapter;

    private PickHallBidingPopWindow statusPopWindow;
    private CommonCategoryFilterPopWindow categoryFilterPopWindow;
    private PickHallBidingPopWindow pickMoneyPopWindow;
    private PickHallBidingPopWindow applyPopWindow;

    private List<PickSelectEntity> typeData = new ArrayList<>();
    private List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> categoryData = new ArrayList<>();
    private List<PickSelectEntity> cityData = new ArrayList<>();

    //省
    private String areacode = "";
    private String areaName;
    //地区
    private String citysNo = "";
    //类型
    private String typeNos = "";
    private String typeName = "";
    private String goodsType = "";
    //招标方式
    private String tenderTypeNo;
    private String tenderTypeName;
    //状态(1、待招标,2、招标中,3、评标中,4、招标结束,5、废标,6、再次招标,7、流标,8、待报名,9、报名中,10、资格审查中)
    private String statusNo = "";
    private String statusName;
    private String moneyNo = "";
    private String moneyName;
    private boolean isChange4;

    private ThreeLevelAreaWindow mAddressPop;
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
    }

    public static PickAttentionSigningUpListFragment newInstance() {
        PickAttentionSigningUpListFragment fragment = new PickAttentionSigningUpListFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_pick_attention_signing_up_list, container, false);
        return binding.getRoot();
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerPickProjectComponent
                .builder()
                .appComponent(appComponent)
                .pickProjectModule(new PickProjectModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        initChooseDialog();
        if (pickingAdapter == null) {
            pickingAdapter = new PickHallBidingAdapter(getActivity(), null);
        }

        DividerItemDecoration divider = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.shape_list_divide2));
        binding.recyclerView.addItemDecoration(divider);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(pickingAdapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initFilterData();
            }

            @Override
            public void onLoadMore() {
                viewModel.getNewList(citysNo, typeNos, moneyNo, true, statusNo, tenderTypeNo, areacode, goodsType);
            }
        });

        pickingAdapter.setOnItemClick(new AdapterOnItemClick<TenderListEntity>() {
            @Override
            public void onItemClick(TenderListEntity tenderListEntity, int position) {
                if (tenderListEntity.getStatus().equals("8")) {   //待报名
                    PickNoticeDetailActivity.start(getActivity(),  tenderListEntity.getId() + "");
                } else{
                    TenderDetailsActivity3.start(getActivity(), tenderListEntity.getId() + "", 0);
                }
            }
        });
        pickingAdapter.onIChildClick(new PickHallBidingAdapter.IChildClick() {
            @Override
            public void onClick(TenderListEntity tenderListEntity) {
                if (tenderListEntity.getStatus().equals("8")) {   //待报名
                    PickNoticeDetailActivity.start(getActivity(), tenderListEntity.getId() + "");
                } else{
                    TenderDetailsActivity3.start(getActivity(), tenderListEntity.getId() + "", 0);
                }
            }
        });
    }

    private void initData() {
        binding.tvStatus.setOnClickListener(this);
        binding.tvCategory.setOnClickListener(this);
        binding.tvAddress.setOnClickListener(this);
        binding.tvApply.setOnClickListener(this);
        binding.tvMoney.setOnClickListener(this);
        binding.rlMapList.setOnClickListener(this);
        binding.ivChoose.setOnClickListener(this);

        categoryFilterPopWindow = new CommonCategoryFilterPopWindow(getActivity(), true);
        categoryFilterPopWindow.setBgView(binding.bgPopGrayLayout);
        categoryFilterPopWindow.setResultListener(new CommonCategoryFilterPopWindow.Listener() {

            @Override
            public void setResultListener(String text, String result, String typeNo) {
                goodsType = result;
                typeNos = typeNo;
                typeName = text;

                if(!"全部".equals(typeName)){
                    updateChooseTextStatus(binding.tvCategory,1,typeName);
                    isChange4 = true;
                }else{
                    isChange4 = false;
                    typeName = "招标类型";
                    updateChooseTextStatus(binding.tvCategory,0,typeName);
                }
                initFilterData();
            }

            @Override
            public void setResetResultListener() {
                goodsType = "";
                typeNos = "";
                isChange4 = false;
                typeName = "招标类型";
                updateChooseTextStatus(binding.tvCategory,0,typeName);
            }
        });
        categoryFilterPopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                if (!isChange4) {
                    updateChooseTextStatus(binding.tvCategory,0,typeName);
                } else {
                    updateChooseTextStatus(binding.tvCategory,1,typeName);
                }
            }
        });
        categoryFilterPopWindow.setCategoryCallBack1(new CommonCategoryFilterPopWindow.CategoryCallBack1() {
            @Override
            public void resultPosition1(String posName1, String posName2, String posName3, String posName4, int pos1, int pos2, int pos3, int pos4) {

            }

            @Override
            public void setChildData(String position) {
                viewModel.getTenderGoodsCategoryAllList(position);
            }
        });

        // 招标状态
        statusPopWindow = new PickHallBidingPopWindow(getActivity());
        statusPopWindow.setBgView(binding.bgPopGrayLayout);
        statusPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                updateChooseStatus(PickAttentionCateType.TYPE_BIDDING_STATUS, (Integer) key3);
                initFilterData();
            }
        });
        statusPopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                int value = getItemPos(PickAttentionCateType.TYPE_BIDDING_STATUS);
                updateChooseTextStatus(binding.tvStatus, value > 0 ? 1 : 0, statusName);
            }
        });

        // 金额
        pickMoneyPopWindow = new PickHallBidingPopWindow(getActivity());
        pickMoneyPopWindow.setBgView(binding.bgPopGrayLayout);
        pickMoneyPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                updateChooseStatus(PickAttentionCateType.TYPE_MONEY, (Integer) key3);
                initFilterData();
            }
        });
        pickMoneyPopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                int value = getItemPos(PickAttentionCateType.TYPE_MONEY);
                updateChooseTextStatus(binding.tvMoney, value > 0 ? 1 : 0, moneyName);
            }
        });
        //招标方式
        applyPopWindow = new PickHallBidingPopWindow(getActivity());
        applyPopWindow.setBgView(binding.bgPopGrayLayout);
        applyPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                updateChooseStatus(PickAttentionCateType.TYPE_BIDDING, (Integer) key3);
                initFilterData();
            }
        });
        applyPopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                int value = getItemPos(PickAttentionCateType.TYPE_BIDDING);
                updateChooseTextStatus(binding.tvApply, value > 0 ? 1 : 0, tenderTypeName);
            }
        });


        viewModel.getProvinceData();
        viewModel.getFilterTypeData();
        viewModel.getTenderNewTypeList();
        viewModel.getPriceData();
        viewModel.getFilterStatusData();
        initFilterData();

        initAddressPopup();
    }

    private void initAddressPopup() {
        mAddressPop = new ThreeLevelAreaWindow(getActivity());
        mAddressPop.setMemberHeight();
        mAddressPop.setCitySingle();
        mAddressPop.setProvinceSingle();
        mAddressPop.setShowSimpleName(true);
        mAddressPop.setOnlyShowProAndCity(true);
        mAddressPop.setAllSingleSelect(true);
        mAddressPop.setThreeListIsGone(true);
        mAddressPop.setOnResultClick(new ThreeLevelAreaWindow.OnResultClickProxy() {
            @Override
            protected void result(GoodsSourceLocationEntity entity) {
                areacode = entity.getProvinceNo();
                if (!TextUtils.isEmpty(entity.getProvinceNo())) {
                    areaName = entity.getProvinceName();
                    areacode = entity.getProvinceNo();
                    citysNo = "";
                    if ("0".equals(areacode)) {
                        areaName = "地区";
                    }
                    updateChooseTextStatus(binding.tvAddress, 1, areaName);
                    if (!TextUtils.isEmpty(entity.getCityNo())) {
                        citysNo = entity.getCityNo();
                        areaName = entity.getCityName();
                        viewModel.getAddressCityData(scrData, citysNo);
                        updateChooseTextStatus(binding.tvAddress, 1, areaName);
                    }
                } else {
                    areaName = "地区";
                    areacode = "";
                    citysNo = "";
                    updateChooseTextStatus(binding.tvAddress, 0, areaName);
                }
                initFilterData();
            }
        });

        mAddressPop.setOnDismissListener(() -> {
            updateChooseTextStatus(binding.tvAddress, "地区".equals(areaName) ? 0 : 1, areaName);
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_status:
                List<PickSelectEntity> statusDatas = getItemData(PickAttentionCateType.TYPE_BIDDING_STATUS);
                if (statusDatas != null && statusDatas.size() > 0) {
                    showStatusPopWindow();
                }
                break;
            case R.id.tv_address:
                showProvincePopWindow();
                break;
            case R.id.tv_category:
                if (null != typeData && typeData.size() > 0) {
                    if (TextUtils.isEmpty(typeName)) {
                        typeName = "招标类型";
                    }
                    updateChooseTextStatus(binding.tvCategory, TextUtils.isEmpty(typeNos) ? -1 : -2, typeName);
                    categoryFilterPopWindow.showPopWindow1(binding.tvCategory);
                    if (TextUtils.isEmpty(typeNos)) {
                        categoryFilterPopWindow.setData1(typeData, 0, "",false);
                    } else {
                        categoryFilterPopWindow.setData1(typeData, Integer.valueOf(typeNos), "",false);
                    }

                }
                break;
            case R.id.tv_money:
                List<PickSelectEntity> priceData = getItemData(PickAttentionCateType.TYPE_MONEY);
                if (priceData != null && priceData.size() > 0) {
                    showMoneyPopWindow();
                }
                break;
            case R.id.tv_apply:
                List<PickSelectEntity> applyDatas = getItemData(PickAttentionCateType.TYPE_BIDDING);
                if (null != applyDatas && applyDatas.size() > 0) {
                    showApplyPopWindow();
                }
                break;
            case R.id.iv_choose:
//                mLeftDialog2.initCategory(Integer.parseInt(typeNo), false,false,false);
//                mLeftDialog2.initData(mData, mFilters);
//                mLeftDialog2.showDialog();
                break;
        }
    }

    /**
     * 省份
     */
    private void showProvincePopWindow() {
        if (TextUtils.isEmpty(areaName)) {
            areaName = "地区";
        }
        updateChooseTextStatus(binding.tvAddress, TextUtils.isEmpty(areacode) && TextUtils.isEmpty(citysNo) ? -1 : -2, areaName);
        mAddressPop.showPopwindow0(binding.tvAddress);
    }

    private void showStatusPopWindow() {
        if (TextUtils.isEmpty(statusName)) {
            statusName = "招标状态";
        }
        updateChooseTextStatus(binding.tvStatus, TextUtils.isEmpty(statusNo) ? -1 : -2, statusName);
        statusPopWindow.showPopwindow(binding.tvStatus, 0, 0);
        int value = getItemPos(PickAttentionCateType.TYPE_BIDDING_STATUS);
        statusPopWindow.updateData(value, getItemData(PickAttentionCateType.TYPE_BIDDING_STATUS));
    }

    private void showMoneyPopWindow() {
        if (TextUtils.isEmpty(moneyName)) {
            moneyName = "概算金额";
        }
        updateChooseTextStatus(binding.tvMoney, TextUtils.isEmpty(moneyNo) ? -1 : -2, moneyName);
        pickMoneyPopWindow.showPopwindow(binding.tvMoney, 0, 0);
        int value = getItemPos(PickAttentionCateType.TYPE_MONEY);
        pickMoneyPopWindow.updateData(value, getItemData(PickAttentionCateType.TYPE_MONEY));
    }

    private void showApplyPopWindow() {
        if (TextUtils.isEmpty(tenderTypeName)) {
            tenderTypeName = "招标方式";
            tenderTypeName = "招标方式";
        }
        updateChooseTextStatus(binding.tvApply, TextUtils.isEmpty(tenderTypeNo) ? -1 : -2, tenderTypeName);
        applyPopWindow.showPopwindow(binding.tvApply, 0, 0);
        int value = getItemPos(PickAttentionCateType.TYPE_BIDDING);
        applyPopWindow.updateData(value, getItemData(PickAttentionCateType.TYPE_BIDDING));
    }

    private void initFilterData() {
        viewModel.getNewList(citysNo, typeNos, moneyNo, false, statusNo, tenderTypeNo, areacode, goodsType);
    }

    public void showList(List<TenderListEntity> tenderListEntities, boolean isLoadMore) {
        binding.recyclerView.refreshComplete();
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (tenderListEntities.size() > 0) {
                pickingAdapter.addDatas(tenderListEntities);
            } else {
                binding.recyclerView.setLoadingMoreEnabled(false);
            }
        } else {
            binding.recyclerView.refreshComplete();
            if (tenderListEntities.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                binding.recyclerView.setVisibility(View.VISIBLE);
                pickingAdapter.setDatas(tenderListEntities);
                binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                pickingAdapter.setDatas(new ArrayList<>());
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.recyclerView.setVisibility(View.GONE);
            }
        }
    }

    public void setStatusDatas(List<PickSelectEntity> datas) {
        List<PickSelectEntity> list = new ArrayList<>();
        for (PickSelectEntity entity : datas) {
            if (!entity.getProjectscale().equals("再次招标")) {
                list.add(entity);
            }
        }
        refreshDataInfo(PickAttentionCateType.TYPE_BIDDING_STATUS, "招标状态", list);
    }

    public void setTenderTypeDatas(List<PickSelectEntity> datas) {
        refreshDataInfo(PickAttentionCateType.TYPE_BIDDING, "招标方式", datas);
    }

    public void setPriceDatas(List<PickSelectEntity> datas) {
        refreshDataInfo(PickAttentionCateType.TYPE_MONEY, "概算金额", datas);
    }

    public void setTypeDatas(List<PickSelectEntity> datas) {
        typeData.clear();
        typeData.addAll(datas);
        if (typeData.size() != 0) {
            typeData.get(0).setCheck(true);
            List<PickSelectEntity> list = new ArrayList<>();
            list.addAll(typeData);
            refreshDataInfo(PickAttentionCateType.TYPE_CATEGORY_3, "招标类型", datas);
        }
    }

    public void setCityDatas(List<PickSelectEntity> data) {
        cityData.clear();
        cityData.addAll(data);
        mAddressPop.setData(scrData, false);
    }

    public void setProvinceDatas(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        if (data != null && data.size() > 0) {
            scrData = data;
            mAddressPop.setData(scrData, true);
        } else {
            viewModel.getAddressCityData(scrData, citysNo);
        }
    }

    /**
     * 分类
     */
    public void setTenderGoodsCategoryAllList(List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> datas) {
        categoryFilterPopWindow.setCategoryData(datas);
        categoryData.clear();
        categoryData.addAll(datas);
        if (categoryData.size() != 0) {
            categoryData.get(0).setCheck(true);
            List<PickSelectEntity> list = new ArrayList<>();
            list.addAll(categoryData);
            refreshDataInfo(PickAttentionCateType.TYPE_CATEGORY_0, "类别", list);
        }
    }

    private void setTextViewBg(TextView textView, int bg, int img, int textColor) {
        if (isAdded()) {
            textView.setBackground(getResources().getDrawable(bg));
            Drawable drawable_n = getResources().getDrawable(img);
            drawable_n.setBounds(0, 0, drawable_n.getMinimumWidth(), drawable_n.getMinimumHeight());
            textView.setCompoundDrawables(null, null, drawable_n, null);
            textView.setTextColor(getResources().getColor(textColor));
        }
    }

    private PickChooseLeftDialog2 mLeftDialog2;
    // 数据集
    private SparseArray<PickSelectTextEntity> mData = new SparseArray<>();
    private SparseIntArray mFilters = new SparseIntArray();
    private SparseArray<String> nameArray = new SparseArray<>();

    private void initChooseDialog() {
        nameArray.put(PickAttentionCateType.TYPE_CATEGORY_0, "招标类型");
        nameArray.put(PickAttentionCateType.TYPE_CATEGORY_1, "类别");
        nameArray.put(PickAttentionCateType.TYPE_CATEGORY_2, "类别1");
        nameArray.put(PickAttentionCateType.TYPE_CATEGORY_3, "类别2");
        nameArray.put(PickAttentionCateType.TYPE_BIDDING_STATUS, "招标状态");
        nameArray.put(PickAttentionCateType.TYPE_CITY, "省份");
        nameArray.put(PickAttentionCateType.TYPE_BIDDING, "招标方式");
        nameArray.put(PickAttentionCateType.TYPE_MONEY, "概算金额");
        mLeftDialog2 = new PickChooseLeftDialog2(getActivity(), nameArray);
        mLeftDialog2.setCallBack(this);
    }

    /**
     * 添加数据到中数据聚合
     */
    private void refreshDataInfo(int sort, String name, List<PickSelectEntity> children) {
        clearOtherDataInfo(sort);
        addEntity(sort, children);
    }

    /**
     * 清理关联项为空
     */
    private void clearOtherDataInfo(int sort) {
        switch (sort) {
            case PickAttentionCateType.TYPE_CATEGORY_0:
                addEntity(PickAttentionCateType.TYPE_CATEGORY_1, new ArrayList<>());
                addEntity(PickAttentionCateType.TYPE_CATEGORY_2, new ArrayList<>());
                addEntity(PickAttentionCateType.TYPE_CATEGORY_3, new ArrayList<>());
                break;
            case PickAttentionCateType.TYPE_CATEGORY_1:
                addEntity(PickAttentionCateType.TYPE_CATEGORY_1, new ArrayList<>());
                addEntity(PickAttentionCateType.TYPE_CATEGORY_2, new ArrayList<>());
                break;
            case PickAttentionCateType.TYPE_CATEGORY_2:
                addEntity(PickAttentionCateType.TYPE_CATEGORY_2, new ArrayList<>());
                break;
            case PickAttentionCateType.TYPE_PROVINCE:
                addEntity(PickAttentionCateType.TYPE_CITY, new ArrayList<>());
                break;
        }
    }

    public void addEntity(int index, List<PickSelectEntity> list) {
        if (mData.get(index) != null) {
            if (list == null || list.size() == 0) {
                mData.get(index).setPickSelectEntityList(new ArrayList<>());
            } else {
                mData.get(index).setPickSelectEntityList(list);
            }
        } else {
            mData.put(index, new PickSelectTextEntity(index, nameArray.get(index), list));
        }
        mFilters.put(index, 0);
    }

    @Override
    public void loadCategory(PickSelectTextEntity entity, int itemPos) {
        List<PickSelectEntity> children = new ArrayList<>();
        if (itemPos > 0) {
            switch (entity.getSort()) {
                case PickAttentionCateType.TYPE_CATEGORY_0:
                    viewModel.getTenderGoodsCategoryAllList(entity.getItemNo(itemPos));
                    break;
                case PickAttentionCateType.TYPE_CATEGORY_1:
                    if (categoryData == null || categoryData.size() == 0) {
                        return;
                    }
                    List<PickSelectEntity<List<PickSelectEntity>>> cate1 = categoryData.get(itemPos).getChildren();
                    if (cate1 != null && cate1.size() > 0) {
                        children.addAll(cate1);
                    }
                    mLeftDialog2.clearOtherDataInfo(entity.getSort());
                    mLeftDialog2.addEntity(PickAttentionCateType.TYPE_CATEGORY_1, children);
                    mLeftDialog2.showDialog();
                    break;
                case PickAttentionCateType.TYPE_CATEGORY_2:
                    if (categoryData == null || categoryData.size() == 0) {
                        return;
                    }
                    int firPos = mLeftDialog2.getItemPos(0);
                    List<PickSelectEntity> cate2 = categoryData.get(firPos).getChildren().get(itemPos).getChildren();
                    if (cate2 != null && cate2.size() > 0) {
                        children.addAll(cate2);
                    }
                    mLeftDialog2.clearOtherDataInfo(entity.getSort());
                    mLeftDialog2.addEntity(PickAttentionCateType.TYPE_CATEGORY_2, children);
                    mLeftDialog2.showDialog();
                    break;
                case PickAttentionCateType.TYPE_PROVINCE:
//                    viewModel.getCityData(entity.getItemNo(itemPos), true);
                    break;
            }
        } else {
            if (entity.getSort() == PickAttentionCateType.TYPE_CATEGORY_0 || entity.getSort() == PickAttentionCateType.TYPE_CATEGORY_1 || entity.getSort() == PickAttentionCateType.TYPE_CATEGORY_3 || entity.getSort() == PickAttentionCateType.TYPE_PROVINCE) {
                mLeftDialog2.clearOtherDataInfo(entity.getSort());
                mLeftDialog2.showDialog();
            }
        }
    }

    @Override
    public void loadData(SparseArray<PickSelectTextEntity> data, SparseIntArray map) {
        mData = PickCopyUtils.deepCopy(data);
        mFilters = map.clone();
        for (int i = 0; i < mData.size(); i++) {
            int key = mData.keyAt(i);
            PickSelectTextEntity entity = mData.get(key);
            updateChooseStatus(entity.getSort(), mFilters.get(entity.getSort()));
        }
        initFilterData();
    }

    @Override
    public void reset() {
        mFilters.clear();
        clearOtherDataInfo(PickAttentionCateType.TYPE_CATEGORY_0);
        clearOtherDataInfo(PickAttentionCateType.TYPE_PROVINCE);
        loadData(mData, mFilters);
        mLeftDialog2.initData(mData, mFilters);
        mLeftDialog2.showDialog();
    }

    @Override
    public void onCancel() {
        mLeftDialog2.initData(mData, mFilters);
    }

    /**
     * 获取当前缓存位置下标
     *
     * @param pos index
     */
    public int getItemPos(int pos) {
        return mFilters.get(pos);
    }

    public List<PickSelectEntity> getItemData(int index) {
        if (mData.get(index) != null) {
            return mData.get(index).getPickSelectEntityList();
        }
        return new ArrayList<>();
    }

    /**
     * 筛选赋值
     */
    private void updateChooseStatus(int key, int value) {
        mFilters.put(key, value);
        String no = mData.get(key).getItemNo(value);
        String name = mData.get(key).getItemName(value);
        switch (key) {
            case PickAttentionCateType.TYPE_CATEGORY_0:  //类别
                typeNos = no;
                typeName = name;
                if (TextUtils.isEmpty(no)) {
                    name = "招标类型";
                }
                updateChooseTextStatus(binding.tvCategory, value, name);
                break;
            case PickAttentionCateType.TYPE_CATEGORY_1:  //类别
                goodsType = no;
                typeName = name;
                if (TextUtils.isEmpty(no)) {
                    name = "招标类型";
                }
                updateChooseTextStatus(binding.tvCategory, value, name);
                break;
            case PickAttentionCateType.TYPE_CATEGORY_2:
            case PickAttentionCateType.TYPE_CATEGORY_3:
                if (!TextUtils.isEmpty(no)) {
                    goodsType = no;
                    typeName = name;
                    if (value > 0) {
                        updateChooseTextStatus(binding.tvCategory, value, name);
                    }
                }
                break;
            case PickCateType.TYPE_MONEY: // 概算金额
                moneyNo = no;
                moneyName = name;
                updateChooseTextStatus(binding.tvMoney, value, name);
                break;
            case PickAttentionCateType.TYPE_BIDDING_STATUS: // 招标状态
                statusNo = no;
                statusName = name;
                updateChooseTextStatus(binding.tvStatus, value, name);
                break;
            case PickAttentionCateType.TYPE_BIDDING: // 招标方式
                tenderTypeNo = no;
                tenderTypeName = name;
                updateChooseTextStatus(binding.tvApply, value, name);
                break;
            case PickAttentionCateType.TYPE_PROVINCE: // 省
                areacode = no;
                mAddressPop.setDefPosition(value, 0);
                if (value == 0) {
                    areaName = "地区";
                } else {
                    areaName = name;
                }
                updateChooseTextStatus(binding.tvAddress, value, areaName);
                break;
            case PickAttentionCateType.TYPE_CITY: // 城市
                citysNo = no;
                mAddressPop.setDefPosition(mFilters.get(PickCateType.TYPE_PROVINCE), value);
                if (value > 0) {
                    areaName = name;
                    updateChooseTextStatus(binding.tvAddress, value, areaName);
                }
                break;
        }
    }

    /**
     * 更新选项颜色
     */
    private void updateChooseTextStatus(PickPopWindowView pickPopWindowView, int value, String name) {
        if (!TextUtils.isEmpty(name)) {
            if (name.length() > 4) {
                name = name.substring(0, 4) + "...";
            }
        }
        if (value == -1 || value == -2) {
            binding.popLine.setVisibility(View.VISIBLE);
            pickPopWindowView.setChooseData(R.drawable.member_pop_blue_bg2, R.mipmap.ic_up_arrow_n, R.color.color_333333,true,name);
        }else if (value == 0) {
            binding.popLine.setVisibility(View.GONE);
            pickPopWindowView.setChooseData(R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333,false,name);
        } else {
            binding.popLine.setVisibility(View.GONE);
            pickPopWindowView.setChooseData(R.drawable.search_new_blue_bg_10, R.mipmap.icon_down_red, R.color.color_main,false,name);
        }
    }

    public void getUrl(String url, String id) {
        WebViewX5HostActivity.start(getActivity(), url, id);
    }

}
